package db

import (
	"github.com/beego/beego/v2/adapter/config"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
)

type Mysql struct {
}

func (m *Mysql) GetUrl() string {
	mysqlConf, err := config.NewConfig("ini", "conf/mysql.conf")
	if err != nil {
		logs.Error("加载mysql配置文件出错", err)
		panic("加载mysql配置文件失败")
	}
	host := mysqlConf.String("host")
	username := mysqlConf.String("username")
	password := mysqlConf.String("password")
	db := mysqlConf.String("db")
	port := mysqlConf.String("port")
	return username + ":" + password + "@tcp(" + host + ":" + port + ")/" + db + "?charset=utf8&loc=Asia%2fShanghai"
}

func (m *Mysql) GetDriverName() string {
	return "mysql"
}

func (m *Mysql) GetAliasName() string {
	return "default"
}

func (m *Mysql) GetDriver() orm.DriverType {
	return orm.DRMySQL
}

func GetMysqlConfig() *Mysql {
	return &Mysql{}
}
